package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import db.BCrypt;
import db.HibernateUtil;
import db.Tootaja;
import db.Yyrnik;


/**
 * Servlet implementation class LoginController
 */
@WebServlet("/login")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher view = request.getRequestDispatcher("/login.jsp");
		HttpSession session = request.getSession();
		if(session.getAttribute("user") != null){
			session.removeAttribute("user");
		}
		if(session.getAttribute("admin") != null){
			session.removeAttribute("admin");
		}
		view.forward(request, response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    RequestDispatcher view = request.getRequestDispatcher("/login.jsp");
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			Session s = sessionFactory.openSession();
			s.beginTransaction();
			Yyrnik yyrnik =  (Yyrnik) s.createQuery("from Yyrnik where kasutajanimi='"+request.getParameter("kasutajanimi")+"'").uniqueResult();
			if(yyrnik != null){
				if(BCrypt.checkpw(request.getParameter("parool"), yyrnik.getParool())){
					request.getSession().setAttribute("user", yyrnik);
					if(session.getAttribute("error") != null){
						session.removeAttribute("error");						
					}
					response.sendRedirect("/websocket/pealeht");
					return;
				}else{
					session.setAttribute("error", "Vale parool!");								
				}			
			}else{
			    Tootaja tootaja =  (Tootaja) s.createQuery("from Tootaja where kasutajanimi='"+request.getParameter("kasutajanimi")+"'").uniqueResult();
			    if(tootaja != null ){
				if(BCrypt.checkpw(request.getParameter("parool"), tootaja.getParool())){
					request.getSession().setAttribute("admin", tootaja);
					if(session.getAttribute("error") != null){
						session.removeAttribute("error");						
					}
					response.sendRedirect("/websocket/admin");
					return;
				}else{
					session.setAttribute("error", "Vale parool!");								
				}
			    }
			session.setAttribute("error", "Sellise kasutajanimega kasutaja puudub!");
			}			
		view.forward(request, response); 
	}

}
